﻿Imports System.Data.SqlClient
Public Class DbConnect
    Private conn As New SqlConnection
    Private conStr As String
    Public Sub New()

    End Sub
    Public Sub New(ByVal server As String, ByVal database As String)
        conStr = "server=" & server & ";database=" & database & ";integrated security=true"
        conn.ConnectionString = conStr
    End Sub
    Public Sub New(ByVal server As String, ByVal database As String, ByVal uid As String, ByVal pwd As String)
        conStr = "server=" + server + ";database=" + database + ";uid=" + uid + ";pwd=" + pwd
        conn.ConnectionString = conStr
    End Sub
    Public Function connect() As Boolean
        Try
            conn.Open()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function QueryTable(ByVal queryStr As String) As DataTable
        Dim adapter As SqlDataAdapter

        adapter = New SqlDataAdapter(queryStr, conn)
        Dim dt As DataTable
        dt = New DataTable
        adapter.Fill(dt)
        Return dt
    End Function

    Public Function Query(ByVal queryStr As String) As Boolean
        'Try
        If conn.State <> ConnectionState.Open Then conn.Open()
        Dim comm As SqlCommand
        comm = New SqlCommand(queryStr, conn)

        comm.ExecuteNonQuery()
        Return True
        'Catch ex As Exception
        '    Return False
        'End Try

    End Function
    Public Function CloseConnect() As Boolean
        Try
            conn.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    ''' <summary>
    ''' kiem tra ket noi den server
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IsConnected() As Boolean
        Return conn.State = ConnectionState.Open
    End Function
End Class
